package library.and.xps.com.step.stepmodule;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import library.and.xps.com.step.App;
import library.and.xps.com.step.PedoConstant;
import library.and.xps.com.step.util.StepManager;

public class StepServiceDelegate implements Handler.Callback {
    private long TIME_INTERVAL = 500;
    private Messenger mGetReplyMessenger;
    private Handler mDelayHandler;
    private Messenger mMessenger;
    private StepListener stepListener;
    private Context mContext;
    int lastStep = 0;
    int initCount = 0;//初始化步数

    private StepServiceDelegate() {

        mContext = App.getInstance();

        mGetReplyMessenger = new Messenger(new Handler(this));
        mDelayHandler = new Handler(this);
        Intent intent = new Intent(mContext, StepService.class);
        mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE);
        mContext.startService(intent);
    }

    //取消绑定服务
    public void callunbindService() {
        try {
            Message msg1 = Message.obtain(null, PedoConstant.MSG_FROM_CLIENT_UNBIND_SERVICE);
            msg1.replyTo = mGetReplyMessenger;
            mMessenger.send(msg1);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    //绑定服务
    public void callbindService() {
        try {
            Message msg1 = Message.obtain(null, PedoConstant.MSG_FROM_CLIENT_BIND_SERVICE);
            msg1.replyTo = mGetReplyMessenger;
            mMessenger.send(msg1);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    //初始化步数
    public void initStep(int initStep) {
        this.initCount = initStep;
    }

    private static StepServiceDelegate stepServiceDelegate;

    public static StepServiceDelegate getInstance() {
        if (stepServiceDelegate == null) {
            stepServiceDelegate = new StepServiceDelegate();
        }
        return stepServiceDelegate;
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case PedoConstant.MSG_FROM_SERVER:
                int step = msg.getData().getInt("step");
                if (step > lastStep) {
                    lastStep = step;
                    initCount++;
                }

                stepListener.step(initCount);
                StepManager.getInstance().updateStep(initCount);
                mDelayHandler.sendEmptyMessageDelayed(PedoConstant.REQUEST_SERVER, TIME_INTERVAL);
                break;
            case PedoConstant.REQUEST_SERVER:
                if (mMessenger != null) {
                    try {
                        Message msg1 = Message.obtain(null, PedoConstant.MSG_FROM_CLIENT);
                        msg1.replyTo = mGetReplyMessenger;
                        mMessenger.send(msg1);

                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
        }
        return false;
    }

    public void setStepListener(StepListener stepListener) {
        this.stepListener = stepListener;
    }

    public interface StepListener {
        /**
         * 在产生有效的一步的时候回调
         */
        void step(int step);
    }

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                mMessenger = new Messenger(service);
                Message msg = Message.obtain(null, PedoConstant.MSG_FROM_CLIENT);
                msg.replyTo = mGetReplyMessenger;
                if (mMessenger != null)
                    mMessenger.send(msg);

            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

}
